﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppConfig;

namespace AllInOne
{
    public static class DomainManager
    {
        public static Dictionary<string, CSharpAppAdapter> AppAdapters { get; private set; }

        static DomainManager()
        {
            AppAdapters = new Dictionary<string, CSharpAppAdapter>();
        }

        public static CSharpAppAdapter Start(string name)
        {
            if (AppAdapters.ContainsKey(name))
            {
                return AppAdapters[name]; 
            }
            else
            {
                var domain = AppDomain.CreateDomain(name);
                domain.SetupInformation.ShadowCopyFiles = "True";

                var type = typeof(CSharpAppAdapter);
                var adapter = domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName) as CSharpAppAdapter;

                AppAdapters.Add(name, adapter);
                return adapter;
            }
        }

    }
}
